package com.nifostasky.pattern.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 直连交换机配置
 *  路由模式
 */
@Configuration
public class RabbitDirectConfig {
    //直连交换机
    public static final String EXCHANGE_NAME = "DIRECT_EXCHANGE";
    //队列
    public static final String QUEUE_NAME_INSERT = "DIRECT_QUEUE_INSERT";
    public static final String QUEUE_NAME_UPDATE = "DIRECT_QUEUE_UPDATE";

    /**
     * 1.交换机
     */
    @Bean(EXCHANGE_NAME)
    public Exchange bootExchange() {
        return ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 2.Queue insert队列
     *  创建队列1
     */
    @Bean(QUEUE_NAME_INSERT)
    public Queue bootQueueInsert() {
        return QueueBuilder.durable(QUEUE_NAME_INSERT).build();
    }

    /**
     * 2.Queue update队列
     *  创建队列2
     */
    @Bean(QUEUE_NAME_UPDATE)
    public Queue bootQueueUpdate() {
        return QueueBuilder.durable(QUEUE_NAME_UPDATE).build();
    }

    /**
     * 3. 绑定insert 队列1
     * 3. routing key: insert
     *  这里的routingKey 不能随意乱写
     */
    @Bean
    public Binding bindInsertDirectExchange(@Qualifier(QUEUE_NAME_INSERT) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("insert").noargs();
    }

    /**
     * 3. 绑定update 队列2
     * 3. routing key: update
     * 这里的routingKey 不能随意乱写
     */
    @Bean
    public Binding bindUpdateDirectExchange(@Qualifier(QUEUE_NAME_UPDATE) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("update").noargs();
    }
}
